package cn.edu.ncut.cs.springboot.petmanagementsystem.service.Impl;

import cn.edu.ncut.cs.springboot.petmanagementsystem.mapper.EmployeeMapper;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.Employee;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.StaffService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Slf4j
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean createStaff(Employee employee) {
        if (employee == null || !StringUtils.hasText(employee.getEmployeeName())) {
            throw new IllegalArgumentException("员工姓名不能为空");
        }

        employee.setStatus(employee.getStatus() == null ? 1 : employee.getStatus());
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        int result = employeeMapper.insert(employee);
        return result > 0;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean updateStaff(Employee employee) {
        if (employee == null || employee.getId() == null) {
            return false;
        }

        employee.setUpdateTime(LocalDateTime.now());
        int result = employeeMapper.updateById(employee);
        return result > 0;
    }

    @Override
    public IPage<Employee> getStaffList(Integer pageNum, Integer pageSize, String employeeName, String department) {
        Page<Employee> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(employeeName)) {
            wrapper.like(Employee::getEmployeeName, employeeName);
        }
        if (StringUtils.hasText(department)) {
            wrapper.eq(Employee::getDepartment, department);
        }
        
        wrapper.orderByDesc(Employee::getCreateTime);
        return employeeMapper.selectPage(page, wrapper);
    }

    @Override
    public Employee getStaffById(Long id) {
        if (id == null) {
            return null;
        }
        return employeeMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean updateSchedule(Long id, String schedule) {
        // 这里可以将排班信息存储在description字段或其他字段中
        // 实际业务中可能需要单独的排班表
        Employee employee = employeeMapper.selectById(id);
        if (employee == null) {
            return false;
        }
        
        // 假设将排班信息存储在description字段
        employee.setDescription(schedule);
        employee.setUpdateTime(LocalDateTime.now());
        int result = employeeMapper.updateById(employee);
        return result > 0;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean deleteStaff(Long id) {
        if (id == null) {
            return false;
        }
        int result = employeeMapper.deleteById(id);
        return result > 0;
    }
}

